package cn.edu.sdjzu.xg2103.controller.basic;

import cn.edu.sdjzu.xg2103.entity.Teacher;
import cn.edu.sdjzu.xg2103.exception.BysjException;
import cn.edu.sdjzu.xg2103.service.TeacherService;
import com.alibaba.fastjson.JSONObject;
import org.w3c.dom.traversal.TreeWalker;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

//put,http://localhost:8080/TeacherUpdate.ctl
@WebServlet("/TeacherUpdate.ctl")
public class TeacherUpdateCtl extends HttpServlet {
    protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        JSONObject message_jsonObj = new JSONObject();
        response.setContentType("application/json;charset=UTF-8");
        try{
            Teacher teacher = new Teacher("A22");
            boolean updated = TeacherService.getInstance().update(teacher);
            message_jsonObj.put("updated",updated);
            message_jsonObj.put("records","修改了一条信息");
            //分页获得所有学院
            //响应message_jsonObj到前端
            response.getWriter().println(message_jsonObj);
        }catch (BysjException e){
            message_jsonObj.put("message",e.getMessage());
            response.getWriter().println(message_jsonObj);
            e.getStackTrace();
        }catch (SQLException e){
            message_jsonObj.put("message","数据库操作异常");
            response.getWriter().println(message_jsonObj);
            e.getStackTrace();
            message_jsonObj.put("message",e.getMessage());
            response.getWriter().println(message_jsonObj);
        }catch (Exception e){
            message_jsonObj.put("message","网络异常");
            response.getWriter().println(message_jsonObj);
            e.getStackTrace();
            System.out.println(e.getMessage());

        }

    }
}
